/*
 * Copyright and license 
 */
package kernel;

import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GAUSSIAN;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;

import java.awt.image.BufferedImage;
import java.util.Map;

import utils.IniFilesReader;

import com.googlecode.javacv.cpp.opencv_core.IplImage;

/**
 * @author JuanK
 *
 */
public class OpenCVProcesor {

	private static OpenCVProcesor instance;
	private String classifierName;
	private OpenCVProcesor()
	{
		try {
			setup();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void setup()throws Exception
	{
		Map<String, String> params = IniFilesReader.readIniFile("./ext/config/javacv.config");
		classifierName = params.get("opencv.data.path");
		classifierName = classifierName.concat(params.get("opencv.classifier"));
	}
	
	public static OpenCVProcesor getInstance()
	{
		if(instance == null)
		{
			instance = new OpenCVProcesor();
		}
		return instance;
	}
	
	public String smooth(String inputFilename, String outputFilename, int cv_size) { 
		System.out.println();
		IplImage image = cvLoadImage(inputFilename);
		if (image != null) {
			cvSmooth(image, image, CV_GAUSSIAN, cv_size);
			//TODO: find out how to do it without using secondary memory for updating the gui
			cvSaveImage(outputFilename, image);
			cvReleaseImage(image);
		}
		return inputFilename + "\n" + outputFilename + "\n" + cv_size;
	}
	
	public void FindFace(String inputFileName)
	{
		
	}
	
	public void smooth(String inputFilename, BufferedImage outputBufferedImage,int cv_size)
	{
		//TODO: there is an issue creating an IplImage with a BufferedImage. Use this code when fixed
		// changing the first parameter to BufferedImage inputBufferedImage
//		IplImage image = IplImage.createFrom(inputBufferedImage);
		IplImage image = cvLoadImage(inputFilename);
		if(image != null)
		{
			cvSmooth(image, image, CV_GAUSSIAN, cv_size);
			image.copyTo(outputBufferedImage);
//			outputBufferedImage = image.getBufferedImage();
			cvReleaseImage(image);
		}
	}
}
